<?php
include_once("includes/common.php");
include_once("class/User.class.php");
require_once("islogin.php");

if ($islogin==1){
    header("location:index.php");
    exit;
}

$pageName = "登录";

$warn = 0;
$error = 0;
$success = 0;
$info = 0;
$phone = null;
$password = null;

if($_GET['msg']=='must'){
    $info = 1;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $phone = $_POST['phone'];
    $password = $_POST['password'];

    if (empty($phone) || empty($password)){
        $warn = 1;
    }else{
        $res = json_decode($user->login($phone, $password),true);
        if($res['status']==200){
            $success = 1;
            setcookie("u_token", $res['message'], time() + (86400 * COOKIE_OVERDUE_DAY), "/");
        }else{
            $error = 1;
        }

    }


}
?>
<!DOCTYPE html>
<html>
    <?php require_once('head.php'); ?>
	<body>
    <?php require_once('header.php'); ?>
		<div class="container-fluid content">
			<div class="row">
                <?php require_once('menu.php'); ?>
				<div class="col watercon">
					<div class="row justify-content-center login_content">
						<div class="col login_left">
							<div class="login_tips">登录后推荐更懂你的笔记</div>
							
							<div class="login_logo"><img src="static/img/logo.png" alt="" /></div>
							
							<div class="login_qrcode"><img src="/api/qrcode.php?url=<?php echo urlencode("小蓝书毕设作品 开发起始日期：2024年9月6日 - 2024年10月13日 作者QQ：575732022");?>" /></div>
							
							<div class="login_can">
								可用<span class="weightfont"><img class="smallsvg" src="favicon.ico"> 小蓝书</span>或<span class="weightfont"><img class="smallsvg" src="static/testcontenimg/weixin.svg" /> 微信</span>扫码
							</div>
							
						</div>
						<div class="col login_right">
							<div class="login_right_title">手机号登录</div>
							<div class="login_form">
								<form action="" method="post" class="form" id="loginform">
									<div class="login_phone">
										<label class="phone">
											<span class="country-code">+86</span>
											<input maxlength="11" value="<?php echo $phone;?>" class="phoneInput" placeholder="输入手机号" type="text" name="phone">
										</label>
									</div>
									<div class="login_pwd">
										<label class="pwd">
											<input maxlength="100" value="<?php echo $password;?>" name="password" class="pwdInput" type="password" placeholder="输入密码" >
										</label>
									</div>
									<div class="login">
										<button class="login_btn" <?php if ($success==1){echo "disabled";}?>> 登录 </button>
									</div>
								</form>
							</div>
							<div class="login_agree">
									<input checked="checked" class="agreements" type="checkbox" name=""  id="mustcheck" />
									<span class="agreement-text">
									我已阅读并同意<a target="_blank" href="#">《用户协议》</a><a target="_blank" href="#">《隐私政策》</a><a target="_blank" href="#">《儿童/青少年个人信息保护规则》</a>
								 </span>
							</div>
							<div onclick="window.location.href='reg.php'" id="loginReg" class="login_reg"><img id="userIcon" src="static/img/user-gray.svg" />新用户注册</div>
						</div>
					</div>


				</div>
			</div>
		</div>

        <script>
            let uid = '';
            let apiType = '';
            let page = 1;
            let keyword = '';
            let note_type = 3;
        </script>
        <script src="static/js/jquery.min.js"></script>
        <script src="static/js/login.js"></script>
        <script src="static/js/custom.js"></script>
        <script src="static/js/message.min.js"></script>
        <script>
            /*qmsg js*/
            Qmsg.config({
                position:'right'
            });

            // 获取表单和复选框的元素
            const form = document.getElementById('loginform');
            const checkbox = document.getElementById('mustcheck');

            // 表单提交时触发的事件
            form.addEventListener('submit', function(event) {
                // 检查复选框是否被选中
                if (!checkbox.checked) {
                    // 如果复选框未选中，阻止表单提交
                    event.preventDefault();
                    Qmsg.warning("请先阅读并同意《用户协议》!");
                }
            });

            <?php if($warn==1){?>
            Qmsg.warning("手机号和密码不得为空!");
            <?php }?>

            <?php if($error==1){?>
            Qmsg.error("<?php echo $res['message']; ?>");
            <?php }?>

            <?php if($info==1&&$success==0&&$error==0&&$warn==0){?>
            Qmsg.info("必须先登录才能进行此操作！");
            <?php }?>

            <?php if($success==1){?>
                Qmsg.success("登录成功，自动跳转中 . . .",{
                    position: 'center'
                });


                setTimeout(function() {
                    window.location.href = 'index.php';
                }, 1700);
            <?php }?>

        </script>
	</body>
</html>